package com.hy.mydesktop.shared.util.log;


import java.security.AccessController;
import java.text.MessageFormat;
import java.util.Date;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;

import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.hy.mydesktop.client.mvc.base.BaseController;
import com.hy.mydesktop.client.mvc.base.BasePresenterAndView;

import sun.security.action.GetPropertyAction;


/**
 * 
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2011-9-13；时间：上午10:03:18</li>
 * <li>类型名称：MyLoggerUtil</li>
 * <li>设计目的：使用Gwt+JdkLogger的工具类</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class MyLoggerUtil {

	public static void startJdkLogger() {
		  Logger rootLogger = Logger.getLogger("");
		 // rootLogger.setLevel(Level.FINE);

		  Handler[] handlers = rootLogger.getHandlers();
			//System.out.println("handlers size :"+handlers.length);

			for (int i = 0; i < handlers.length; i++) {
				//System.out.println(handlers[i].getClass());
				//System.out.println(handlers[i].getFormatter().getClass());
				handlers[i].setFormatter(new MyGwtFormatter());
			}
	}
	
	private static void printDebug(String className,String message,Throwable throwable) {
		StackTraceElement[] stackTraceElements= throwable.getStackTrace();
		/*System.out.println("size :"+stackTraceElements.length);
		for (int s = 0; s < stackTraceElements.length; s++) {
		      System.out.println(s + " " + stackTraceElements[s].getClassName()
		          + stackTraceElements[s].getClassName() + stackTraceElements[s].getLineNumber());
		    }
		 System.err.println(str);*/
		String str = "" + "(" + stackTraceElements[1].getFileName()+":" + stackTraceElements[1].getLineNumber()+")";
		Logger logger = Logger.getLogger(className);		  
		// logger.log(Level.SEVERE,message+" " +str );
		 String formatMessage = message +" " +str;
		 printMessage(className, formatMessage, Level.FINE);
		
	}
	
	private static void printInfo(String className,String message,Throwable throwable) {
		StackTraceElement[] stackTraceElements= throwable.getStackTrace();
		/*System.out.println("size :"+stackTraceElements.length);
		for (int s = 0; s < stackTraceElements.length; s++) {
		      System.out.println(s + " " + stackTraceElements[s].getClassName()
		          + stackTraceElements[s].getClassName() + stackTraceElements[s].getLineNumber());
		    }
		 System.err.println(str);*/
		String str = "" + "(" + stackTraceElements[1].getFileName()+":" + stackTraceElements[1].getLineNumber()+")";
		Logger logger = Logger.getLogger(className);		  
		// logger.log(Level.SEVERE,message+" " +str );
		 String formatMessage = message +" " +str;
		 printMessage(className, formatMessage, Level.FINE);
		
	}
	
	public static void printFine(String className,String message) {
		
		printMessage(className, formatMessage(message), Level.FINE);
	}
	
	public static void printInfo(Class className,String message) {
		printMessage(className.toString(), formatMessage(message), Level.INFO);

	}
	
	public static void printInfo(String className,String message) {
		printMessage(className, formatMessage(message), Level.INFO);

	}
	
	
	
	public static void printWarn(Class className,String message) {
		printWarn(className.getName(), message);

	}
	
	public static void printWarn(String className,String message) {
		printMessage(className, formatMessage(message), Level.WARNING);

	}
	
	public static void printSevere(String className,String message) {
		printMessage(className, formatMessage(message), Level.SEVERE);

	}
	
	public static void printError(String className,String message) {
		printSevere(className, message);
		
	}
	
	public static void printError(Class className,String message) {
		printSevere(className.getClass().getName(), message);
		
	}
	
	public static void printWarning(Class className,String message) {
		printWarn(className.getName(), message);
		
	}
	
	public static void printWarning(String className,String message) {
		printWarn(className, message);
		
	}
/////////////////////////////////////////////////////////////////////	
	public static void printPresenterDebug(Class className,String eventName) {
		printDebug(className.getName(), "该Persenter，响应 , "+eventName+" 事件");
	}
	
	public static void printPresenterDebugForInit(Class className) {
		printDebug(className.getName(), "创建  "+getClassShortName(className));
	}
////////////////////////////////////////////////////////////////////////	
	public static void printComponentDebugForEventTrigger(Class className,String eventName) {
		printDebug(className.getName(), "该控件，触发 "+eventName+" 事件");
	}
	public static void printControllerDebugForInit(Class className) {
		//int lastIndex=getlastIndexOfPoint(className.getName());
		//String shortName = getClassShortName(className);
//		printDebug(className.getName(), "创建控制器:  "+className.getName().substring(lastIndex+1));
		printDebug(className.getName(), "创建控制器:  "+getClassShortName(className));
	}
	

///////////////////////////////////////////////////////////////////////	
	
	public static void printControllerDebugForEvent(Class className,String eventName) {
		printDebug(className.getName(), "该控制器，响应 ,并转发 "+eventName+" 事件");
	}
	public static void printComponentDebugForInit(Class className) {
		printDebug(className.getName(), "创建控件:  "+getClassShortName(className));
	}
	
///////////////////////////////////////////////////////////////////////	
	public static void printDebugForBaseModels(Class className,BaseModel... baseModels) {
		StringBuffer message=new StringBuffer();

		for (BaseModel baseModel : baseModels) {
			message.append(baseModel+"\n");
		}
		printDebug(className.getName(), message.toString());
	}
	
	public static void printDebugForBaseModelAttributes(Class className,BaseModel baseModel) {
		printDebugForBaseModelAttributes(className.toString(), baseModel);
		
	}
	public static void printErrorForBaseModelAttributes(Class className,BaseModel baseModel) {
		
		printError(className,createBaseModelMessage( baseModel));

	}
	public static void printDebugForBaseModelAttributes(String className,BaseModel baseModel) {
		
			/*StringBuffer message=new StringBuffer();
			for (String propertyName : baseModel.getPropertyNames()) {
				message.append("属性名称:"+propertyName+",属性值:"+baseModel.get(propertyName)+"\n");
			}*/						
			printDebug(className,createBaseModelMessage( baseModel));
	}
	
	public static String createBaseModelMessage(BaseModel baseModel) {
		
		StringBuffer message=new StringBuffer();
		for (String propertyName : baseModel.getPropertyNames()) {
			message.append("属性名称:"+propertyName+",属性值:"+baseModel.get(propertyName)+"\n");
		}						
		return message.toString();
	}
	
	public static void printDebug(Class className,String message) {
		printDebug(className.getName(), message);
	}
	public static void printDebug(String className,String message) {
		printFine(className, message);
		
	}
	private static void printMessage(String className,String message,Level level) {
		
		Logger logger = Logger.getLogger(className);	  
		 logger.log(level,message );
		
	}
	
	
	public static String formatMessage(String message) {
		
		
		Throwable throwable = new Throwable();
		StackTraceElement[] stackTraceElements= throwable.getStackTrace();
		//System.out.println("size :"+stackTraceElements.length);
		int index = 0;
		for (int s = 1; s < stackTraceElements.length; s++) {
		     // System.out.println(s + " className :" + stackTraceElements[s].getClassName()
		      //    +" methodName :"+ stackTraceElements[s].getMethodName() + stackTraceElements[s].getLineNumber());
			if (stackTraceElements[s].getMethodName().indexOf("print")!=0) {
				break;
			} else {
				++index;
			}
		}
		++index;
		 //System.err.println(str);
		String str = "" + "(" + stackTraceElements[index].getFileName()+":" + stackTraceElements[index].getLineNumber()+")";
		return message +" " +str;
	//	return str +" " + message;
		
	}

	public static void printAsteriskDebug(
			Class clazz) {
		printDebug(clazz, "***************************************************************");
	}
	
	public static int getlastIndexOfPoint(String className) {
		return className.lastIndexOf(".");
	}
	
	public static String getClassShortName(Class className) {
		//int lastIndex=getlastIndexOfPoint(className.getName());
			return className.getName().substring(getlastIndexOfPoint(className.getName())+1);
	}

	public static void printContollerAnndComponentDebug(
			Class className, Controller controller,
			BaseController parentController, BasePresenterAndView presenter) {
		printDebug(className, "控制器"+getClassShortName(controller.getClass())+
				"(对应控件"+getClassShortName(presenter.getDisplay().getComponent().getClass())+")"+
				"的父控制器是"+getClassShortName(parentController.getClass())+"(对应控件"+getClassShortName(((LayoutContainer)presenter.getDisplay().getComponent().getParent()).getClass())+")");
		
	}

	

}
